<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2006 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */

/**
 * Set of methods used in the views.
 */
class Admin_Admin
{
    /**
     * Initialize the admin.
     *
     * Loads all the app.php files for each installed application.
     */
    public static function start()
    {
        $GLOBALS['_PX_Admin_apps'] = array();
        foreach (Pluf::f('installed_apps', array()) as $app) {
            if (false !== ($file = Pluf::fileExists($app.'/papp.php'))) {
                $_app = include $file;
                $GLOBALS['_PX_Admin_apps'][$_app['path']] = $_app;
            }
        }
    }

    /**
     * Get the model name from the application path and model path.
     *
     * @param string Application path
     * @param string Model path
     * @return string Model name or false
     */
    public static function getModelName($app_path, $model_path)
    {
        if (isset($GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path]['model'])) {
            return $GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path]['model'];
        }
        return false;
    }

    /**
     * Get the model details
     *
     * @param string Application path
     * @param string Model path
     * @param string Key or all the data (null)
     * @param string Default value if not found (false)
     * @return string Model name or false
     */
    public static function getModelDetails($app_path, $model_path, $key=null,
                                           $default=false)
    {
        if ($key !== null and isset($GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path][$key])) {
            
            return $GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path][$key];
        } elseif ($key === null and isset($GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path])) {
            return $GLOBALS['_PX_Admin_apps'][$app_path]['models'][$model_path];
        } 
        return $default;
    }

    /**
     * Get installed applications
     */
    public static function getInstalledApp()
    {
        return $GLOBALS['_PX_Admin_apps'];
    }

}
